import os
import sys
# import torch
import csv
# from getfacelabel import *
from audiohandler.task2handler import Task2Handler

class MatchHandler:

    def __init__(self, audio_path, label, match_table):
        
        self.audio_path = audio_path
        self.label = label
        self.feature = None
        self.match_list = []
        self.match_table = match_table

    def make_match_list(self, audio_id):

        '''
        audio_id: int
        '''
        video_name = 'combine' + str(audio_id).zfill(3) + '.mp4'
        person_id_list = self.label[video_name]
        return person_id_list

    def do_match_single(self, audio_id):

        person_id_list = self.make_match_list(audio_id)
        audio_handler = Task2Handler(3)
        audio_handler.test_init()
        score_all = []
        for sub_audio_idx in range(3):
            wav_file = str(audio_id).zfill(3) + '_' + str(sub_audio_idx+1) + '.wav'
            person_id, score_list = audio_handler.test_single_audio(os.path.join(self.audio_path, wav_file))
            score_all.append(score_list)
        # print(score_all)
        '''
        score_all:
        [
            [score from ID1 to ID20 of audio 1]
            [score from ID1 to ID20 of audio 2]
            [score from ID1 to ID20 of audio 3]
        ]
        '''
        match_score_list = []
        '''
        现在有1、2、3三段音频和a、b、c三个ID
        其中，a、b、c按照左中右已经排好
        按照以下顺序进行计算
        | a | b | c | id |
        | 1 | 2 | 3 | 0  |
        | 1 | 3 | 2 | 1  |
        | 2 | 1 | 3 | 2  |
        | 2 | 3 | 1 | 3  |
        | 3 | 1 | 2 | 4  |
        | 3 | 2 | 1 | 5  |
        '''

        # print(person_id_list)
        compute_table = []

        with open(self.match_table) as csvfile:
            csv_reader = csv.reader(csvfile)
            for row in csv_reader:
                compute_table.append(row)

        for match_single in compute_table:

            if match_single == ['id', 'left', 'middle', 'right']:
                continue
            
            left_score_list = score_all[int(match_single[1])-1]
            middle_score_list = score_all[int(match_single[2])-1]
            right_score_list = score_all[int(match_single[3])-1]

            single_score = left_score_list[int(person_id_list[0][2:])-1] + \
                            middle_score_list[int(person_id_list[1][2:])-1] + \
                            right_score_list[int(person_id_list[2][2:])-1]
            
            match_score_list.append(single_score)
        match_info = compute_table[match_score_list.index(max(match_score_list))+1]
        # print(match_info)

        # print('audio {} is left, audio {} is middle, audio {} is right'.format(match_info[1], match_info[2], match_info[3]))
        # print(person_id_list)
        audio_name_left = str(audio_id).zfill(3) + '_' + match_info[1] + '.wav'
        audio_name_middle = str(audio_id).zfill(3) + '_' + match_info[2] + '.wav'
        audio_name_right = str(audio_id).zfill(3) + '_' + match_info[3] + '.wav'
        match_result = [[audio_name_left, audio_name_middle, audio_name_right],
                        person_id_list]

        return match_result



# if __name__ == '__main__':
#     label = get_face_label('../data/facedata.json')
#     handler = MatchHandler('../result', label, './matchtable.csv')
#     handler.do_match_single(1)
